// src/store/actions/auth.action.ts
export const SIGNUP = 'SIGNUP'
export const SIGN_SUCCESS = 'SIGN_SUCCESS'
export const SIGN_FAIL = 'SIGN_FAIL'
export const RESET_SIGNUP = 'RESET_SIGNUP'

// 发起注册请求时的参数接口类型
export interface SignupPayload {
    email: string,
    name: string,
    password: string
}

// 发起注册请求时的 dispatch 类型及参数
export interface SignupAction {
    type: typeof SIGNUP,
    payload: SignupPayload
}

// 注册请求成功时的 dispatch 类型
export interface SignupSuccessAction {
    type: typeof SIGN_SUCCESS
}

// 注册请求失败时的 dispatch 类型及错误信息
export interface SignupFailAction {
    type: typeof SIGN_FAIL,
    message: string
}

// 重置注册状态
export interface ResetSignupAction {
    type: typeof RESET_SIGNUP
}

// 获取需要执行加载的 dispatch 的参数
export const signup = (payload: SignupPayload): SignupAction => ({
    type: SIGNUP,
    payload
})

// 获取需要执行注册成功的 dispatch 的参数
export const signupSuccess = (): SignupSuccessAction => ({ type: SIGN_SUCCESS })

// 获取需要执行注册失败的 dispatch 的参数
export const signupFail = (message: string): SignupFailAction => ({
    type: SIGN_FAIL,
    message
})

// 获取重置注册状态的 dispatch 的参数
export const resetSignup = (): ResetSignupAction => ({
    type: RESET_SIGNUP
})

/**
 * 登录
 */

export const SIGNIN = 'SIGNIN'
export const SIGNIN_SUCCESS = 'SIGNIN_SUCCESS'
export const SIGNIN_FAIL = 'SIGNIN_FAIL'

// 发起登录请求时的参数接口类型
export interface SigninPayload {
    email: string,
    password: string
}

// 发起登录请求时的 dispatch 类型及参数
export interface SigninAction {
    type: typeof SIGNIN,
    payload: SigninPayload
}

// 登录成功时的 dispatch 类型
export interface SigninSuccessAction {
    type: typeof SIGNIN_SUCCESS
}

// 登录失败时的 dispatch 类型及参数
export interface SigninFailAction {
    type: typeof SIGNIN_FAIL,
    message: string
}

// 获取登录请求时的 dispatch 的参数
export const signin = (payload: SigninPayload): SigninAction => ({
    type: SIGNIN,
    payload
})

// 获取登录成功时的 dispatch 的参数
export const signinSuccess = (): SigninSuccessAction => ({
    type: SIGNIN_SUCCESS
})

// 获取登录失败时的 dispatch 的参数
export const signinFail = (message: string): SigninFailAction => ({
    type: SIGNIN_FAIL,
    message
})

export type AuthUnionType = 
    SignupAction | 
    SignupSuccessAction | 
    SignupFailAction | 
    ResetSignupAction |
    SigninAction |
    SigninSuccessAction | 
    SigninFailAction